﻿//-----------------------------------------------------------------------
// <copyright file="WebflowActionAttribute.cs" company="Sławomir Rosiek">
//     Copyright (c) Sławomir Rosiek. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Rosieks.Web.Flow
{
    using System.Web.Mvc;

    public class WebflowActionAttribute : WebflowAccessableAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            var webflowContext = WebflowContext.GetCurrent(filterContext.HttpContext);

            RouteData routeData;
            if (!webflowContext.IsCurrentStateValid(filterContext.RequestContext, out routeData))
            {
                filterContext.Result = routeData.AsRedirectResult();
            }
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var result = filterContext.Result as WebflowResult;
            if (result != null)
            {
                var webflowContext = WebflowContext.GetCurrent(filterContext.HttpContext);

                webflowContext.OnAction(result.ActionName, filterContext);
            }

            base.OnActionExecuted(filterContext);
        }
    }
}
